package com.txx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.txx.mapper.GoodsDescMapper;
import com.txx.mapper.GoodsMapper;
import com.txx.mapper.GoodsParamMapper;
import com.txx.pojo.Goods;
import com.txx.pojo.GoodsDesc;
import com.txx.pojo.GoodsParam;
import com.txx.util.Constants;
import com.txx.vo.GoodsParamVO;
import com.txx.vo.GoodsVO;
import com.txx.vof.GoodsForm;
import com.txx.vor.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: zgc
 * @Date: 2021/5/21 9:12
 */

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsDescMapper goodsDescMapper;

    @Autowired
    private GoodsParamMapper goodsParamMapper;

    //设置为常量
    private static final ObjectMapper MAPPER = new ObjectMapper();
    @Override
    public PageResult findGoodsList(GoodsForm goodsForm) {
        IPage page = new Page(goodsForm.getPage(),
                goodsForm.getPageSize());
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        //用户是否传递参数
        boolean flag = StringUtils.hasLength(goodsForm.getName());
        queryWrapper.like(flag,"name", goodsForm.getName());

        page = goodsMapper.selectPage(page,queryWrapper);
        long total = page.getTotal();
        List<Goods> itemList = page.getRecords();
        return new PageResult(){{
            setTotalCount(total);
            setList(itemList);
        }};
    }

    @Override
    public int Update(Goods goods) {
        return goodsMapper.updateById(goods);
    }

    @Override
    public int Insert(GoodsVO goodsVO) {

        System.out.println(goodsVO);
        //1.入库商品表
        Goods goods = goodsVO.getGoods();
        goods.setStatus(true);   //默认启用状态
        goods.setIsDelete(Constants.IsDelete.Off.ordinal());
        //要求入库之后返回主键
        //MP如果设定了主键自增则会自动的实现数据回显
        goodsMapper.insert(goods);

        //2.入库商品详情
        GoodsDesc goodsDesc = goodsVO.getGoodsDesc();
        goodsDesc.setId(goods.getId());
        goodsDescMapper.insert(goodsDesc);

        //3.入库商品参数
        //1.一个商品应该有自己的单独的参数. 动态参数/静态属性 KYE-VLAUE [key:value,key2:value2]
        GoodsParam goodsParam = goodsVO.getGoodsParam();
        GoodsParamVO[] dynamic = goodsParam.getDynamicArray();
        GoodsParamVO[] staticParam  = goodsParam.getStaticArray();
        try {
            //将页面传递的数据转化为JSON,之后数据库保存
            String dynamicJSON = MAPPER.writeValueAsString(dynamic);
            String staticJSON = MAPPER.writeValueAsString(staticParam);
            //封装商品参数信息
            goodsParam.setDynamicArgs(dynamicJSON)
                    .setStaticArgs(staticJSON)
                    .setId(goods.getId());
            //实现入库操作
            goodsParamMapper.insert(goodsParam);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            //如果程序执行报错 则抛出异常
            throw new RuntimeException(e);
        }

        return 1;
    }
}
